import pymysql
from db.dbconfig import *
import pyecharts
def insert_db(sql_insert,params):
    db= pymysql.connect(host = db_address,
                        port = db_port,
                        user = db_username,
                        passwd = db_password,
                        db = db_name)

    #使用cursor()方法获取操作游标

    cur = db.cursor()
    try:
        cur.execute(sql_insert,params)
        db.commit()
    except Exception as e:
        print("错误信息：%s" % str(e))
        db.rollback()
    finally:
        db.close()

def select_db(sql):
    '''查询数据库'''
    # 打开数据库连接
    db = pymysql.connect(host=db_address,
                         port=db_port,
                         user= db_username,
                         passwd= db_password,
                         db= db_name)

    # 使用 cursor() 方法创建一个游标对象cur
    cur = db.cursor()
    # 使用 execute()  方法执行 SQL 查询
    cur.execute(sql)
    # 使用 fetchall() 方法获取查询结果
    data = cur.fetchall()
    # print(data)  # 取出对应的psw值
    # 关闭数据库连接
    db.close()
    return data

def deleteAll():
    db = pymysql.connect(host=db_address,
                         port=db_port,
                         user=db_username,
                         passwd=db_password,
                         db=db_name)
    sql = "truncate table t_china_covid_data"
    cur = db.cursor()
    cur.execute(sql)
    db.close()